package com.server.llh.llhserverwork.activity.infomation;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.WebViewActivity;
import com.server.llh.llhserverwork.activity.base.LlhBaseActivity;
import com.server.llh.llhserverwork.adapter.ZiXunHolder;
import com.server.llh.llhserverwork.adapter.base.ListViewDataAdapter;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.ZongheZxBena;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ZTF on 2017-06-06.
 */

public class InfoMationActivity extends LlhBaseActivity {
    @BindView(R.id.titleName)
    TextView titleName;
    @BindView(R.id.main_back)
    ImageButton mainBack;
    @BindView(R.id.leftBtn_RL_title_base)
    RelativeLayout leftBtnRLTitleBase;
    @BindView(R.id.serverName)
    TextView serverName;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    @BindView(R.id.order_list)
    ListView orderList;
    @BindView(R.id.refresh_data)
    SwipeRefreshLayout refreshData;
    private static final String TAG = "LlhInfomation";
    private String mUserIdStr, mCenterId, mCenterType, currentOrgName, type, token;
    private List<ZongheZxBena.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        titleName.setText("资讯");
        mainBack.setVisibility(View.GONE);
        token = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        mUserIdStr = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.USER_ID, getApplicationContext());
        mCenterId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID, getApplicationContext());
        mCenterType = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_TYPE, getApplicationContext());
        currentOrgName = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_NAME, getApplicationContext());
        reqInfomationData(mCenterId, mCenterType, currentOrgName);
        orderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = dataBeanList.get(position).getMessage_url();
                Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    private ZongheZxBena zongheZxBena;
    private void reqInfomationData(String mOrgId, String mOrgType, String currentOrgName) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("token", token);
        if (!"1".equals(type)) {
            lRequest.setUri(ServerPort.ACTIVITY_INFO_MATION_DATA);
            lRequest.setUriType(UrlsTools.MSG_INFO);
        } else {
            lRequest.addParam("currentOrgId", mOrgId);
            lRequest.addParam("currentOrgType", mOrgType);
            lRequest.addParam("currentOrgName", currentOrgName);
            lRequest.setUriType(UrlsTools.MSG_ACTIVITY_INFO);
            lRequest.setUri(ServerPort.INFO_MATION_DATA);
        }
        lRequest.setIp(UrlsTools.REQ_URL);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), getResources().getString(R.string.toast_str));
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null) {
                    zongheZxBena = new Gson().fromJson(responseBean, ZongheZxBena.class);
                    dataBeanList = zongheZxBena.getData();
                    ListViewDataAdapter<ZongheZxBena.DataBean> adapter = new ListViewDataAdapter<ZongheZxBena.DataBean>(null);
                    adapter.update(dataBeanList);
                    adapter.setViewHolderClass(null, ZiXunHolder.class, InfoMationActivity.this);
                    orderList.setAdapter(adapter);
                }
            }
        });
    }

    public void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    showProgress(InfoMationActivity.this);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        });
    }

    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    closeProgress();
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        });
    }


    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }


    @Override
    protected int getlayout() {
        return R.layout.llh_my_list;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

}
